<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="net.sf.rose.business.service.DBMetaService" %>
<%@include file="/common/header.jsp"%>
<c:set var="path" value="..${ctx}/define/action"/>

<div class="pageContent">

	<div class="panelBar">
		<ul class="toolBar">
			<li><a class="edit" onclick="save();" title="保存"><span>保存</span></a></li>
			<li class="line">line</li>
			<li><a class="delete" onclick="deleteAction();"><span>删除</span></a></li>
		</ul>
	</div>

	<form name="action_set_form" method="post" action="${path}/save.html" class="pageForm required-validate" onsubmit="return validateCallback(this, dialogAjaxDone);">
		<input type="hidden" id="actionType" name="actionType" value="${actionType}" />
		<div class="pageFormContent" layoutH="260">
			<fieldset>
				<legend>基本信息</legend>
				<dl >
					<dt>操作名称：</dt>
					<dd><input name="name" id="bean_name" type="text" size="30" value="${bean.name}" class="required" /></dd>
				</dl>
				<dl >
					<dt>分类：</dt>
					<dd><input name="sort" type="text" size="30" value="${bean.sort}" class="required" /></dd>
				</dl>
				<dl class="nowrap">
					<dt>描述：</dt>
					<dd><textarea name="description" cols="90" rows="3">${bean.description}</textarea></dd>
				</dl>
			</fieldset>
		</div>

		<div class="pageFormContent" layoutH="160">
		<div class="tabs">
		<div class="tabsHeader">
			<div class="tabsHeaderContent">
				<ul>
					<li class="selected"><a href="javascript:void(0)"><span>操作单元列表</span></a></li>
					<li><a href="javascript:void(0)"><span>参数列表</span></a></li>
				</ul>
			</div>
		</div>
		
		<div class="tabsContent" style="height: 160px;">
			<div>
				<div class="panelBar">
					<ul class="toolBar">
						<li><a class="add" onclick="add('添加操作');" title="添加操作"><span>添加</span></a><br /><br /></li>
						<li class="line">line</li>
						<li><a class="edit" onclick="moveActionUnit('up');" title="上移" ><span>上移</span></a></li>
						<li class="line">line</li>
						<li><a class="edit" onclick="moveActionUnit('down');" title="下移"><span>下移</span></a></li>
						<li class="line">line</li>
						<li><a class="delete" onclick="deleteActionUnit();"><span>删除</span></a></li>
					</ul>
				</div>

				<table class="table" width="100%" >
					<thead>
						<tr>
							<th width="160">操作单元名称</th>
							<th width="80">分类</th>
							<th width="240">描述</th>
						</tr>

					</thead>
						<tbody id="base_define_action_set_list_tbody">
						<c:if test="${! empty bean.actionUnitBeans}">
						<c:forEach var="data" items="${bean.actionUnitBeans}">
						<tr target="params" rel="${data.name}" class="base_define_action_set_sub_unit_list_tr">
							<td>${data.name}</td>
							<td>${data.sort}</td>
							<td>${data.description}</td>
						</tr>
						</c:forEach>
						</c:if>
					</tbody>
				</table>

			</div>

			<div>
				<table class="list nowrap itemDetail" addButton="添加参数" width="100%">
					<thead>
						<tr>
							<th type="text" name="items.key[#index#]" size="25" fieldClass="required">键名</th>
							<th type="text" name="items.value[#index#]" size="25" fieldClass="required">值</th>
							<th type="enum" name="items.type[#index#]" enumUrl="..${ctx}/common/view.html?jsp=define/dbmeta/data_type_list" size="23" fieldClass="required">数据类型</th>
							<th type="text" name="items.format[#index#]" size="30">格式</th>
							<th type="del" width="30">操作</th>
						</tr>
					</thead>
					<tbody>
						<c:if test="${! empty bean.fields }">
						<c:forEach var="data" items="${bean.fields}" varStatus="status">
						<tr>
							<td>
								<input type="text" class="required textInput" size="25" value="${data.key}" name="items.key[${status.index}]" />
							</td>
							<td><input type="text" class="required textInput" size="25" value="${data.value}" name="items.value[${status.index}]" /></td>
							<td>
								<select name="items.type[${status.index}]" class="combox">
									<%request.setAttribute("dataTypes", DBMetaService.getService().getAllDataType());%>
									<c:forEach var="type" items="${dataTypes}">
										<option value="${type}" <c:if test="${type==data.type}">selected</c:if> >${type}</option>
									</c:forEach>
								</select>
							</td>
							<td><input type="text" class="textInput" size="30" value="${data.format}" name="items.format[${status.index}]" /></td>
							<td><a class="btnDel " href="javascript:void(0)">删除</a></td>
						</tr>
						</c:forEach>
						</c:if>			
					</tbody>
				</table>
			</div>
		</div>
		</div>
	</div>

	</form>
</div>
<script type="text/javascript">
/**
 * dialog上的表单提交回调函数
 * 服务器转回navTabId，可以重新载入指定的navTab. statusCode=DWZ.statusCode.ok表示操作成功, 自动关闭当前dialog
 * form提交后返回json数据结构,json格式和navTabAjaxDone一致
 */
function dialogAjaxDone(json){
	DWZ.ajaxDone(json);
	if (json.statusCode == DWZ.statusCode.ok){
		// alertMsg.correct(json.message);
		if(json.callbackType){
			var url = "${path}/list.html";
			navTab.reload(url,null,"base_define_action_unit_list");
			// navTab.reload(url);
			//navTab.reloadFlag("base_define_action_list"); // 下次切换到列表页签时需要刷新一下
			setTimeout(function(){$.pdialog.closeCurrent();}, 100); // 关闭此编辑页签
		}
	}
}

function add(title){
	var url = "..${ctx}/define/actionunit/consult.html?js_back_function=consultBack";
	$.pdialog.open(url, "base_define_action_unit_consult", title,{width:800,height:480,mask:true,maxable:false,resizable:false});
}

function consultBack(args){
	//var p = $("#base_define_action_set_list_tbody");
	//p.append("<tr class='base_define_action_set_list_tr' rel='2' target='params'>"+
	//		"<td style='width: 219px;'><div>2</div></td>"+
	//		"<td style='width: 108px;'><div>2</div></td>"
	//		+"<td style='width: 330px;'><div>2</div></td>"
	//		+"</tr>");
	//$(form).attr("action",$(form).attr("action") + "?actionUnits=" + args);
	//$(form).submit();
	
	//$(action_set_form).attr("action","${path}/saveTemp.html?actionUnits=" + args);
	//$(action_set_form).submit();
	//alert($(action_set_form).serialize());

	var params = $(action_set_form).serialize();
	params = params + "&actionUnits=" + args;
	// serialize()自动调用了encodeURIComponent方法将数据编码了
	// 调用decodeURIComponent(XXX,true);将数据解码
	params = decodeURIComponent(params,true);
	// 将值部分二次转码，以防出现中文乱码的问题
	params = convertUTF8(params);
	var url = "${path}/saveTemp.html?" + params;
	// 关闭子参照窗体
	$.pdialog.close("base_define_action_unit_consult");
	// 重新打开本窗体
	$.pdialog.open(url, "base_define_action_add", "操作",{width:800,height:480,mask:true,maxable:false,resizable:false});

	//$.pdialog.reload(url,{},"base_define_action_add");
	//$.pdialog.close("base_define_action_add");
	
	//$("#base_define_action_add").loadUrl(url,{}, null);
}

/**
 * 将URL请求参数中值的部分二次转码，以防出现中文乱码
 * 例：actionType=&name=q&sort=q&description=q&dwz_rowNum=1&items.key%5B0%5D=q
 */
function convertUTF8(args){
	var params = "";
	var arr = args.split("&");
	for(var s in arr){
		var array = arr[s].split("=");
		if(array[1] != ""){
			params = params + '&' + array[0] + '=' + encodeURI(encodeURI(array[1]));
		}
	}
	return params.substr(1);
}

function save(){
	$(action_set_form).submit();
}

function deleteAction(){
	var rel = '';
	$('.base_define_action_set_list_tr').each(function(){
		if($(this).hasClass("selected")){
			rel = $(this).attr('rel');
		}
	});
	if(rel==''){
		alertMsg.warn('请先选择一条数据！');
	}else{
		alertMsg.confirm("确定要删除吗?", {
			okCall: function(){
				var url = "${path}/delete.html?type=action&id=" + rel;
				$.ajax({
					type: "post", url: url,
					success: function(json){
						alertMsg.correct(json.msg);
						var url = "${path}/list.html";
						navTab.reload(url);
						setTimeout(function(){$.pdialog.closeCurrent();}, 100); // 关闭此编辑页签
					},
					error: function(){
						alertMsg.error('系统错误，请联系管理员！');
					}
				});
			}
		});
	}
}

function deleteActionUnit(){
	var rel = '';
	$('.base_define_action_set_sub_unit_list_tr').each(function(){
		if($(this).hasClass("selected")){
			rel = $(this).attr('rel');
		}
	});
	if(rel==''){
		alertMsg.warn('请先选择一条数据！');
	}else{
		alertMsg.confirm("确定要删除吗?", {
			okCall: function(){
				var url = "${path}/removeActionUnit.html?actionUnitName=" + rel;
				$.pdialog.reload(url,{},"base_define_action_add");
			}
		});
	}
}

function moveActionUnit(type){
	var rel = '';
	$('.base_define_action_set_sub_unit_list_tr').each(function(){
		if($(this).hasClass("selected")){
			rel = $(this).attr('rel');
		}
	});
	if(rel==''){
		alertMsg.warn('请先选择一条数据！');
	}else{
		var url = "${path}/moveActionUnit.html?actionUnitName=" + rel + "&type=" + type;
		$.pdialog.reload(url,{},"base_define_action_add");
	}
}
</script>
